﻿Imports System.Data
Imports System.Data.SqlClient

Partial Class service_forms_one_time_project
    Inherits System.Web.UI.Page

    Dim connString As String = "Data Source=sql2008.campus.jcu.edu ; Initial Catalog=service; User Id=Thunder48; Password=Thundertech169;"

    Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load

        '        Dim parent = CType(Me.Master, driver_MasterPage)

        'Make sure it's not the admin
        If Session("driver_id") Is Nothing Then
            Session("loggedin") = False
            Response.Redirect("default.aspx")
        End If

        If IsPostBack Then
            classInformation()
        End If

        Try
            If IsPostBack = False Then
                'activityList.Items.Clear()
                Dim conn As New SqlConnection
                Dim cmd As String
                Dim sql As SqlCommand = New SqlCommand
                Dim reader As SqlDataReader
                conn.ConnectionString = connString
                conn.Open()
				
                cmd = "select * from COMSERV_ACTIVITY,Event where semester_yearid='" & Session("semester") & "' and needed = 'One-time Project' and COMSERV_ACTIVITY.activityid = Event.ServiceID and DriverID is NULL and COMSERV_ACTIVITY.[begin_date] >= GETDATE() Order by COMSERV_ACTIVITY.[begin_date] asc"
                'cmd = "select * from COMSERV_ACTIVITY where semester_yearid=" & parent.getSemester() & " and needed = 'One-time Project'"
                sql.CommandText = cmd
                sql.Connection = conn
                'sql.Parameters.Clear()
                reader = sql.ExecuteReader()
                If reader.HasRows Then
                    activityList.DataSource = reader
                    activityList.DataTextField = "activity-name"
                    activityList.DataValueField = "activityid"
                    activityList.DataBind()
                End If

                conn.Close()
            End If
        Catch ex As Exception
            Response.Write(ex)
        End Try
        

    End Sub

    Protected Sub classInformation()

        'activityList.Items.Clear()
        Dim conn2 As New SqlConnection
        Dim cmd2 As String
        Dim sql2 As SqlCommand = New SqlCommand
        Dim classInfo As SqlDataReader
        Dim drivetime As Integer
        conn2.ConnectionString = connString
        conn2.Open()
        Dim selection As String = activityList.SelectedValue
        'Response.Write(selection)
        cmd2 = "select [description],[begin_time],[end_time],[travel_time], [activity-name],[begin_date] from comserv_activity where [activityid]=" & selection
		
        'Response.Write(cmd2)
        sql2.CommandText = cmd2
        sql2.Connection = conn2

        Try
            classInfo = sql2.ExecuteReader()
            If classInfo.Read() Then
                drivetime = classInfo.GetInt32(3)
                activityDetails.Text = classInfo.GetString(0)
                name.Text = "<h2>" & classInfo.GetString(4) & "</h2><br>"
                begin.Text = "Begin (with travel): " & (classInfo.GetDateTime(1)).AddMinutes(-(drivetime)).ToString("h:mm tt") & "<br>"
                endt.Text = "End   (with travel): " & (classInfo.GetDateTime(2)).AddMinutes(drivetime).ToString("h:mm tt") & "<br>"
                day.Text = "Day: " & classInfo.GetDateTime(5).ToString("ddd, MMM d") & "<br>"
                'Loop
            End If
        Catch ex As Exception
            'Response.Write(ex)
        End Try

    End Sub

    Protected Sub registerButton_Click(sender As Object, e As System.EventArgs) Handles registerButton.Click

        Try
            Dim conn3 As New SqlConnection
            Dim cmd3 As String
            Dim sql3 As SqlCommand = New SqlCommand
            Dim selection, actIDS As String
            'Dim classInfo As SqlDataReader
            conn3.ConnectionString = connString
            conn3.Open()

            'Response.Write(selection)

            'fname = TextBox1.Text
            'lname = TextBox2.Text


            selection = activityList.SelectedValue
            cmd3 = "update Event set driverID='" & Session("driver_id") & "' where serviceID='" & selection & "'"

            sql3.CommandText = cmd3
            sql3.Connection = conn3

            sql3.ExecuteNonQuery()
        Catch ex As Exception
            'Response.Write(ex)
        Finally
            panel1.Visible = False
            panel2.Visible = True



        End Try

    End Sub
End Class
